package top.lshaci.learning.thread;

/**
 * JoinReleaseLockTest
 *
 * @author lshaci
 * @since 1.0.0
 */
public class JoinReleaseLockTest extends Thread {

    public JoinReleaseLockTest(String name) {
        super(name);
    }

    @Override
    public void run() {
        synchronized (this) {
            for (int i = 0; i < 100; i++) {
                System.out.println(super.getName() + i);
            }
        }
    }

    /**
     * main线程释放了锁t1
     */
    public static void main(String[] args) throws Exception {
        Thread t1 = new JoinReleaseLockTest("t1 -- ");
        t1.start();

        synchronized (t1) {
            for (int i = 0; i < 100; i++) {
                if (i == 20) {
                    try {
                        t1.join();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(Thread.currentThread().getName() + " -- " + i);
            }
        }
    }
}
